package com.tcr.core;

import java.io.File;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;

import com.tcr.dao.DbUtil;

/**
 * Servlet implementation class StartUp
 */
public class StartUp extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StartUp() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		try {
			// disable Freemarker logging SAGOPT-216
			try {
				// not flexible nor beautiful
				freemarker.log.Logger
						.selectLoggerLibrary(freemarker.log.Logger.LIBRARY_JAVA);
			} catch (ClassNotFoundException anException) {
				throw new RuntimeException(anException);
			}
			if (checkDb())
				db.init();
			cron.start();
			log.info("start cron");

		} catch (Exception ex) {
			log.error(ex);
		}
	}

	public boolean checkDb() {
		String db_path = System.getProperty("user.home")
				+ System.getProperty("file.separator") + "tcr.db";
		File db = new File(db_path);
		if (db.exists())
			return false;
		return true;
	}

	private DbUtil db = DbUtil.getInstance();
	private final static Logger log = Logger.getLogger(StartUp.class);
	private UnzipCron cron = new UnzipCron();
}
